import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.util.Calendar;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.JToolBar;
import javax.swing.JOptionPane;
import javax.swing.KeyStroke;
import javax.swing.ListSelectionModel;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.table.DefaultTableModel;

/**
 * 
 * @author Krzysztof Marjański
 * @author Łukasz Gadomski
 * 
 * Główne okno programu. Stanowi to jednocześnie podstawową klasę view.
 *
 */
public class CalendarWindow extends JFrame  implements ActionListener {
	/**Referencja do klasy controllera okna głównego*/
	private Application controller;
	/**Pole widgetu służącego jako kalendarz*/
	private CalendarWidget calendar;
	/**Pole widgetu służacego do prezentacji wydarzeń*/
	private EventsView eventsView;
	private JScrollPane scrollPane;
	/**Widget wyświetlający dodatkowe informacje na temat wybranego dnia*/
	public DayInfo dayInfo;
	private JPanel calendarPanel;
	
	private JMenuBar menuBar;
	/**ComboBox służacy do wyboru aktualnie prezentowanego widoku.*/
	private JComboBox viewSelector;
	private JMenu fileMenu;
	private JMenu help;
	private JMenuItem about;
	private JMenuItem newEvent;
	private JMenu exportSubmenu;
	private JMenuItem toEvolution;
	private JMenuItem toXML;
	private JMenuItem toSQL;
	private JMenu importSubmenu;
	private JMenuItem removeOld;
	private JMenuItem fromXML;
	private JMenuItem fromSQL;
	private JMenuItem settings;
	private JMenuItem exit;
	private JButton add;
	private JButton remove;
	private JButton edit;
	/**Pole do wpisywania filtra wydarzeń.*/
	private JTextField filter;
	
	public static void main(String[] args) throws InterruptedException {
		new Application(args);
	}
	/**
	 * Alokuje obiekt klasy CalendarWindow, inicjalizuje wszystkie potrzebne komponenty oraz ustawia wyświetlaną datę.
	 * @param controler Referencja do controlera okna głównego.
	 * @param date Data do ustawienia jako startowa.
	 */
	public CalendarWindow (Application controler, Calendar date)
	{
		super("Kalendarzyk");
		this.setPreferredSize(new Dimension(250,200));
		this.controller=controler;
		JToolBar toolBar = new JToolBar();
		eventsView = new EventsView();
		calendarPanel = new JPanel (new BorderLayout());
		calendar = new CalendarWidget(controller,date);
		dayInfo = new DayInfo();
		scrollPane = new JScrollPane(eventsView);
		eventsView.setCellSelectionEnabled(true);
		eventsView.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		createMenu ();
		setJMenuBar(menuBar);
		createToolBar(toolBar);
		setLayout(new BorderLayout());
		add(scrollPane,BorderLayout.CENTER);
		calendarPanel.add(calendar,BorderLayout.NORTH);
		calendarPanel.add(dayInfo,BorderLayout.CENTER);
		add(calendarPanel,BorderLayout.EAST);
		add(toolBar, BorderLayout.PAGE_START);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setSize(new Dimension(900, 425));
        setVisible(true);

	}
	/**
	 * Tworzy menu
	 */
	private void createMenu ()
	{
		menuBar = new JMenuBar();
		menuBar.add(fileMenu= new JMenu("Plik"));
		menuBar.add(help = new JMenu("Pomoc"));
		help.add(about = new JMenuItem("O programie"));
		fileMenu.add(newEvent = new JMenuItem("Nowe wydarzenie"));
		fileMenu.add(exportSubmenu = new JMenu("Eksport"));
		exportSubmenu.add(toEvolution = new JMenuItem("do iCalendar"));
		exportSubmenu.add(toXML = new JMenuItem("do XML"));
		exportSubmenu.add(toSQL = new JMenuItem("do SQL"));
		fileMenu.add(importSubmenu = new JMenu("Import"));
		importSubmenu.add(fromXML = new JMenuItem ("z XML"));
		importSubmenu.add(fromSQL = new JMenuItem ("z SQL"));
		fileMenu.add(removeOld = new JMenuItem ("Usuń stare wpisy"));
		fileMenu.add(settings = new JMenuItem("Ustawienia"));
		fileMenu.add(exit = new JMenuItem("Wyjście"));
		newEvent.addActionListener(this);
		removeOld.addActionListener(this);
		toEvolution.addActionListener(this);
		toXML.addActionListener(this);
		toSQL.addActionListener(this);
		fromXML.addActionListener(this);
		fromSQL.addActionListener(this);
		settings.addActionListener(this);
		exit.addActionListener(this);
		about.addActionListener(this);
		fileMenu.setMnemonic(KeyEvent.VK_P);
		help.setMnemonic(KeyEvent.VK_C);
		newEvent.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N, ActionEvent.CTRL_MASK));
		settings.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_P, ActionEvent.CTRL_MASK));
		exit.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Q, ActionEvent.CTRL_MASK));
		about.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O, ActionEvent.CTRL_MASK));
		toEvolution.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_I, ActionEvent.CTRL_MASK));
		toXML.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X, ActionEvent.CTRL_MASK));
		toSQL.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S, ActionEvent.CTRL_MASK));
		fromXML.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X, ActionEvent.SHIFT_MASK));
		fromSQL.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S, ActionEvent.SHIFT_MASK));
		removeOld.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_D, ActionEvent.CTRL_MASK));
		
	}
	/**
	 * Tworzy Toolbar
	 * @param t
	 */
	private void createToolBar (JToolBar t)
	{
		t.setFocusable(true);
		t.add(add = new JButton("Dodaj"));
		t.add(edit =new JButton("Edytuj"));
		t.add(remove = new JButton ("Usuń"));
		t.add (new JLabel ("filtr: "));
		filter = new JTextField();
		filter.setColumns(10);
        t.add(filter);
        filter.getDocument().addDocumentListener(new DocumentListener()
        {
			public void changedUpdate(DocumentEvent e) {
				controller.newFilter(filter.getText());
			}
			public void insertUpdate(DocumentEvent e) {
				controller.newFilter(filter.getText());
				
			}
			public void removeUpdate(DocumentEvent e) {
				controller.newFilter(filter.getText());
			}
			});
        viewSelector = new JComboBox ();
        viewSelector.addItem("Widok standardowy");
        viewSelector.addItem("Sortuj po nazwie");
        viewSelector.addItem("Sortuj po miejscu");
        viewSelector.addItem("Sortuj po dacie początku wydarzenia");
        t.add(viewSelector);
        viewSelector.addActionListener(this);
        add.addActionListener(this);
        edit.addActionListener(this);
        remove.addActionListener(this);
        add.setMnemonic(KeyEvent.VK_D);
        edit.setMnemonic(KeyEvent.VK_E);
        remove.setMnemonic(KeyEvent.VK_U);
        
		
	}
	/**
	 * Ustawia podany model w tabeli eventsView oraz determinuje sposób wyświetlania
	 * @param tmodel Referencja do modelu, który ma być ustawiony.
	 * @param type	Typ wyświetlania; dozwolone wartości: EventsView.HOUR_VIEW i EventsView.SORTED_VIEW
	 */
	public void setModel (DefaultTableModel tmodel, int type)
	{
		eventsView.setModel(tmodel,type);
	}
	/**
	 * Nasłuchuje przyciski oraz menu w oknie głównym. Wywołuje odpowiednie metody z controllera.
	 */
	public void actionPerformed(ActionEvent evt) {
		if (evt.getSource() == about)
		{
			JOptionPane.showMessageDialog(null, "Organizer v1.0\nAutorzy: Marjański, Gadomski", "O programie Organizer", JOptionPane.INFORMATION_MESSAGE);
			return;
		}
		if (evt.getSource() == add)
		{
		  controller.addNewEvent(eventsView.getSelectedRow(),eventsView.getSelectedColumn());
		  return;
		}
		if (evt.getSource()==edit)	
		{
		 controller.editEvent(eventsView.getSelectedRow(),eventsView.getSelectedColumn());
		 return;
		}
		if (evt.getSource()==remove)
		{
			controller.removeEvent(eventsView.getSelectedRow(),eventsView.getSelectedColumn());
			return;
		}
		if (evt.getSource()==viewSelector)
		{
			controller.setView(viewSelector.getSelectedIndex());
			return;
		}
		if (evt.getSource()==newEvent) {
		   controller.addNewEvent(eventsView.getSelectedRow(),eventsView.getSelectedColumn());
		  return;}
		if (evt.getSource()==toEvolution){controller.toVCalendar(); return;};
		if (evt.getSource()==toXML)
		{controller.toXML();
				return;
		}

		if (evt.getSource()==toSQL){controller.toSQL(); return;}
		if (evt.getSource()==fromXML){controller.fromXML(); return;}
		if (evt.getSource()==fromSQL){controller.fromSQL(); return;}
		if (evt.getSource()==settings){controller.settings(); return;}
		if (evt.getSource()==toSQL){controller.toSQL(); return;}
		
		if (evt.getSource()==fromXML)
		{
			
				controller.fromXML();
		}
		if (evt.getSource()==fromSQL)
		{
			controller.fromSQL();
		}
		if (evt.getSource()==settings){controller.settings(); return;};
		if (evt.getSource()==exit){dispose(); System.exit(0);}
		if (evt.getSource()==removeOld) {controller.removeOld(); return;}

		
	}




}
